package cn.itcast.com.network.tcp.exer03;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        //创建socket对象,这个指定的端口号ip都是本身的
        ServerSocket serversocket = new ServerSocket(8282);
        //使用accept，接受请求，并建立连接
        //阻塞
        Socket socket = serversocket.accept();
        //正式开始数据传输
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        //创建一个文件字节输出流
        FileOutputStream fos = new FileOutputStream("a.txt");

        String line;
        while ((line = bufferedReader.readLine()) != null){
            fos.write(line.getBytes());
            fos.write(System.lineSeparator().getBytes());
        }
        bufferedReader.close();
        fos.close();
        socket.close();
        serversocket.close();
    }
}
